package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.DeviceType;
import com.cloudfun.campusshare.common.model.vo.FinishFixVO;
import com.cloudfun.campusshare.common.model.vo.PersonPointVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.WorkerOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/5/11 14:49
 * @description :
 */
@RestController
@RequestMapping("/v1/workerOrder")
@Api(value = "工单管理")
@Validated
@Slf4j
@AuthRequired
public class WorkerOrderController {

    @Autowired
    private WorkerOrderService workerOrderService;

    @ApiOperation(value = "查询工单列表",notes = "查询工单列表")
    @GetMapping("/list")
    public BaseResult queryWorkerOrderList(Pageable pageable,
                                           @ApiParam(value = "时间") @RequestParam(required = false) Integer time,
                                           @ApiParam(value = "洗衣点id集合") @RequestParam(required = false) Set<String> washPointIds,
                                           @ApiParam(value = "关键字") @RequestParam(required = false)String keyword,
                                           @ApiParam(value = "工单状态") @RequestParam(required = false)Set<Integer> status,
                                           @ApiParam(value = "工单任务类型") @RequestParam(required = false)Set<Integer> taskType,
                                           @ApiParam(value = "商户id") @RequestParam(required = false)String merchantId,
                                           @ApiParam(value = "设备种类") @RequestParam(required = false)String productType,
                                           @ApiParam(value = "产品类型") @RequestParam(required = false) String deviceType){
        return BaseResultFactory.produceSuccess(workerOrderService.queryWorkerOrder(pageable,time,washPointIds,keyword,status,taskType,merchantId,productType,deviceType));

    }

    @ApiOperation(value = "查询工单详情",notes = "查询工单详情")
    @GetMapping("/detail/{workerOrderId}")
    public BaseResult queryWorkerOrderDetail(@PathVariable String workerOrderId){
        return BaseResultFactory.produceSuccess(workerOrderService.queryWorkerOrderDetail(workerOrderId));
    }

    @ApiOperation(value = "工单指派",notes = "工单指派")
    @PostMapping("/point")
    public BaseResult personPoint(@RequestBody PersonPointVO personPointVO){
        workerOrderService.workerOrderPoint(personPointVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "检修完成",notes = "检修完成")
    @PostMapping("/finishFix")
    public BaseResult finishFix(@RequestBody FinishFixVO finishFixVO){
        workerOrderService.finishFix(finishFixVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "根据工单状态分组查询数量",notes = "根据工单状态分组查询数量")
    @GetMapping("/count/groupBy/status")
    public BaseResult countGroupByStatus(@ApiParam(value = "运营商id（管理员可指定）") @RequestParam(required = false) String merchantId,
                                         @ApiParam(value = "时间") @RequestParam(required = false) Integer time,
                                         @ApiParam(value = "工单任务类型") @RequestParam(required = false)Integer taskType,
                                         @ApiParam(value = "设备种类") @RequestParam(required = false)String productType,
                                         @ApiParam(value = "产品类型") @RequestParam(required = false) String deviceType){
        return BaseResultFactory.produceSuccess(workerOrderService.countGroupByStatus(merchantId,time,taskType,productType,deviceType));
    }

    @ApiOperation(value = "领取工单",notes = "领取工单")
    @PostMapping("/offer/{workerOrderId}")
    public BaseResult offerWorkerOrder(@PathVariable String workerOrderId){
        workerOrderService.offerWorkerOrder(workerOrderId);
        return BaseResultFactory.produceSuccess();
    }

}
